package com.itlong.cloud.controller.sharedELevator;

import com.alibaba.fastjson.JSON;
import com.itlong.cloud.POJO.DTO.property.PaymentRecordDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.PropertyPaymentRecordVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.PropertyErrorCodeEnum;
import com.itlong.cloud.paymenthub.IPropertyPaymentServiceClient;
import com.itlong.cloud.project.property.IPropertyRoomInfoServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.export.ExportUtil;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      物业缴费管理控制器
 * </desc>
 *
 * @createDate2020/7/15
 **/
@RestController
@RequestMapping("/sharedElevator/payment")
public class PropertyPaymentController {

    @Autowired
    private IPropertyPaymentServiceClient iPropertyPaymentServiceClient;

    @Autowired
    private IPropertyRoomInfoServiceClient iPropertyRoomInfoServiceClient;   //房屋信息服务接口协议提供对象.

    @Value("${" + PlatformConstants.FILE_FTP_HOST + "}")
    public String FILE_FTP_HOST;

    @Value("${" + PlatformConstants.FILE_FTP_PORT + "}")
    public String FILE_FTP_PORT;

    @Value("${" + PlatformConstants.FILE_FTP_USERNAME + "}")
    public String FILE_FTP_USERNAME;

    @Value("${" + PlatformConstants.FILE_FTP_PASSWORD + "}")
    public String FILE_FTP_PASSWORD;

    @Value("${" + PlatformConstants.FILE_ACCESS_URL + "}")
    public String FILE_ACCESS_URL;

    /**
     * <desc>
     *      分页获取缴费记录
     * </desc>
     *
     * @param paymentRecordDTO 缴费记录DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/7/15
     */
    @RequestMapping(value = "/getPage", method = RequestMethod.POST)
    public Object getPage(PaymentRecordDTO paymentRecordDTO, HttpServletRequest request){
        try{
            paymentRecordDTO = HttpProtocolUtil.parseRequestParamToDTO(PaymentRecordDTO.class, request);
            if (paymentRecordDTO == null || StringUtils.isBlank(paymentRecordDTO.getProjectId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<PropertyPaymentRecordVO> page = iPropertyPaymentServiceClient.getPage(paymentRecordDTO);
            String totalAmount = iPropertyPaymentServiceClient.getPaymentPriceCount(paymentRecordDTO);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("totalAmount", totalAmount);
            return new MessageVO(JSON.toJSONString(resultMap), BaseErrorCodeEnum.SUCCESS.getErrorCode(), page);
        }catch (Exception e){
            throw new DataAccessException("【物业缴费管理_分页获取消费记录getPage】分页获取缴费记录失败",e);
        }
    }

    /**
     * <desc>
     *      获取房间剩余次数
     * </desc>
     *
     * @param paymentRecordDTO 缴费记录DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/7/15
     */
    @ResponseBody
    @RequestMapping(value = "/getRoomCount", method = RequestMethod.POST)
    public Object getRoomCount(PaymentRecordDTO paymentRecordDTO, HttpServletRequest request){
        try{
            paymentRecordDTO = HttpProtocolUtil.parseRequestParamToDTO(PaymentRecordDTO.class, request);
            if (paymentRecordDTO == null || StringUtils.isBlank(paymentRecordDTO.getRoomId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), (Object)iPropertyRoomInfoServiceClient.getRoomLastCountByRoomId(paymentRecordDTO.getRoomId()));
        }catch (Exception e){
            throw new DataAccessException("【物业缴费管理_getRoomCount】线下缴费失败",e);
        }
    }

    /**
     * <desc>
     *      共享电梯线下缴费
     * </desc>
     *
     * @param paymentRecordDTO 缴费记录DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/7/15
     */
    @ResponseBody
    @AuthAccessRequired
    @RequestMapping(value = "/outLinePay", method = RequestMethod.POST)
    @LoggerInfo(operateDesc = LogDescConstant.PROPERTY_MEAL_MANAGE_MOVE_LOG_DESC)
    public Object outLinePay(PaymentRecordDTO paymentRecordDTO, HttpServletRequest request){
        try{
            paymentRecordDTO = HttpProtocolUtil.parseRequestParamToDTO(PaymentRecordDTO.class, request);
            if (paymentRecordDTO == null || StringUtils.isBlank(paymentRecordDTO.getProjectId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (StringUtils.isNotBlank(paymentRecordDTO.getMealId())) {
                if (iPropertyPaymentServiceClient.checkMealInfo(paymentRecordDTO.getMealId(),paymentRecordDTO.getRoomId()) > 0) {
                    return new MessageVO(PropertyErrorCodeEnum.ELEVATOR_MEAL_TYPE_IS_USE_ERR.getErrorCode());
                }
            }
            iPropertyRoomInfoServiceClient.qrCodeAuthForNew(paymentRecordDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【物业缴费管理_outLinePay】线下缴费失败",e);
        }
    }

    /**
     * <desc>
     *      线下退费
     * </desc>
     *
     * @param paymentRecordDTO 缴费记录DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/7/15
     */
    @ResponseBody
    @AuthAccessRequired
    @RequestMapping(value = "/outLineRefund", method = RequestMethod.POST)
    @LoggerInfo(operateDesc = LogDescConstant.PROPERTY_SHRAED_ELEVATOR_REFUND_LOG_DESC)
    public Object outLineRefund(PaymentRecordDTO paymentRecordDTO, HttpServletRequest request){
        try{
            paymentRecordDTO = HttpProtocolUtil.parseRequestParamToDTO(PaymentRecordDTO.class, request);
            if (paymentRecordDTO == null || StringUtils.isBlank(paymentRecordDTO.getProjectId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            iPropertyRoomInfoServiceClient.outLineRefund(paymentRecordDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【物业缴费管理_outLineRefund】线下退费失败",e);
        }
    }

    /**
     * <desc>
     *      导出缴费记录
     * </desc>
     *
     * @return
     * @Author Zhu.yj
     * @createDate  2020/7/20
     */
    @RequestMapping(value = "export", method = RequestMethod.POST)
    public Object export(PaymentRecordDTO paymentRecordDTO, HttpServletRequest request){
        try{
            paymentRecordDTO = HttpProtocolUtil.parseRequestParamToDTO(PaymentRecordDTO.class, request);
            if (paymentRecordDTO == null || StringUtils.isBlank(paymentRecordDTO.getProjectId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<PropertyPaymentRecordVO> page = iPropertyPaymentServiceClient.getPage(paymentRecordDTO);
            List<PropertyPaymentRecordVO> list = page.getRows();
            if (list == null || list.isEmpty()){
                return new MessageVO(PropertyErrorCodeEnum.EXPORT_DATA_EMPTY_ERR.getErrorCode());
            }
            for (PropertyPaymentRecordVO propertyPaymentRecordVO : list){
                propertyPaymentRecordVO.setPaymentType(propertyPaymentRecordVO.getPaymentTypeString(propertyPaymentRecordVO.getPaymentType()));
                propertyPaymentRecordVO.setRecordType(propertyPaymentRecordVO.getRecordTypeString(propertyPaymentRecordVO.getRecordType()));
                propertyPaymentRecordVO.setPaymentAmount("¥"+propertyPaymentRecordVO.getPaymentAmount()+"元");
                propertyPaymentRecordVO.setPaymentCount(propertyPaymentRecordVO.getPaymentCount()+"次");
            }
            String exportItemNames = "序号,房间号,记录类型,金额,次数,创建时间,缴费方式,操作人";
            String exportItems = "serialNum,roomId,recordType,paymentAmount,paymentCount,createTime,paymentType,operateUserName";
            String exportFileUrl = ExportUtil.exportByItemsAndNames(list, exportItemNames, exportItems,
                    (short) 420, 16, paymentRecordDTO.getProjectId(), FILE_FTP_HOST, FILE_FTP_USERNAME, FILE_FTP_PASSWORD, FILE_FTP_PORT, FILE_ACCESS_URL);
            if (StringUtils.isBlank(exportFileUrl)){
                return new MessageVO(PropertyErrorCodeEnum.PROJECT_EXPORT_FILE_SAVE_ERR.getErrorCode());
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), (Object) exportFileUrl);
        }catch (Exception e){
            throw new DataAccessException("【物业缴费管理_export】导出缴费记录失败", e);
        }
    }
}
